package com.one.service.impl;

import com.one.bean.OrderInfo;
import com.one.bean.Orders;
import com.one.constant.RabbitMQConstant;
import com.one.constant.RedisPrefixConstant;
import com.one.constant.RequestReturnConstant;
import com.one.mapper.*;
import com.one.producer.RabbitMQTopicProducer;
import com.one.service.CommonOrderService;
import com.one.util.RedisUtils;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.ProductParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Create on 2022-01-18
 * <p>
 * Author: LiangWei
 * <p>
 * Description: 生成普通订单接口
 **/
@Service
public class CommonOrderServiceImpl implements CommonOrderService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RabbitMQTopicProducer rabbitMQTopicProducer;
    @Override
    public RequestReturn<String> createCommonTradeOrder(Integer userId, List<ProductParamVo> productParamVoList) {
        //1.将这个订单里的所有的商品生成一个订单号，准备入redis缓存
        String tradeOrderNo = "T"+ DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now())+ UUID.randomUUID().toString().substring(0,6);
        //用循环将每件商品作为一个个的订单详情插入数据库的orders表中（运费暂时不插入，商品的状态后面要一起变动）
        for(int i=0;i<productParamVoList.size();i++) {
            ProductParamVo productParamVo = productParamVoList.get(i);
            BigDecimal singlePrice = productMapper.selectById(productParamVo.getProdId()).getPrice();
            //初始化一个空的orders
            Orders order = new Orders();
            order.setOrderNo(tradeOrderNo);//插入订单编号
            order.setTotalPrice(singlePrice);//插入这件商品的价格（实际上是这件商品的单价）
            order.setUserId(userId);//插入用户的id
            order.setStatus(1);//将这件商品的订单详情里的状态置为待支付1
            order.setTagColor(productParamVo.getColorId());//这件商品的颜色id
            order.setTagSize(productParamVo.getSizeId());//这件商品的大小id
            order.setProductId(productParamVo.getProdId());//插入商品id
            //**计算这件商品的积分并插入
            Integer integral = singlePrice.intValue() / 100 * 10;
            order.setGetIntegral(integral);
            order.setCreateDate(new Date());//插入这件商品的创建时间
            order.setProdNum(productParamVo.getProductNum());//插入这件商品的数量
            orderMapper.insert(order);
        }
        //初始化一个空的OrderInfo
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo(tradeOrderNo);//插入订单编号
        orderInfo.setCreateDate(new Date());//插入订单创建时间
        orderInfo.setPostage(new BigDecimal("5"));//运费统一价5元
        orderInfo.setStatus(1);//将订单的状态置为待支付1
        orderInfoMapper.insert(orderInfo);

            Map<String,Object> directMap = new HashMap<>();
            directMap.put("OrderNo",tradeOrderNo);
            //存放订单编号到redis中
            redisUtils.set(RedisPrefixConstant.ORDER_NO+String.valueOf(userId)+":"+tradeOrderNo,1,60);
            //发送死信队列绑定的直连队列
            rabbitMQTopicProducer.sendDead(RabbitMQConstant.RABBITMQ_DIRECT_EXCHANGE,RabbitMQConstant.RABBITMQ_DIRECT_KEY,directMap);
        return RequestReturnFactory.success(RequestReturnConstant.RETURN_SUCCESS_CODE,RequestReturnConstant.CREATE_ORDER_SUCCESS_MSG,tradeOrderNo);
    }
}
